﻿/*
 *  Copyright (c) 2009 Alessandro Pilotti
 *
 *  Permission is hereby granted, free of charge, to any person
 *  obtaining a copy of this software and associated documentation
 *  files (the "Software"), to deal in the Software without
 *  restriction, including without limitation the rights to use,
 *  copy, modify, merge, publish, distribute, sublicense, and/or sell
 *  copies of the Software, and to permit persons to whom the
 *  Software is furnished to do so, subject to the following
 *  conditions:
 *  
 *  The above copyright notice and this permission notice shall be
 *  included in all copies or substantial portions of the Software.
 *  
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 *  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 *  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 *  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 *  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 *  OTHER DEALINGS IN THE SOFTWARE.
 */

using System;
using AlexPilotti.KeyStorageManager;
using System.Reflection;

namespace AlexPilotti.KeyStorageManager.Utility
{
    class Program
    {
        static IKeyStorageProvider GetKeyStorageProvider()
        {
            IKeyStorageProvider provider = KeyStorageManager.GetProvider();

            if (provider == null)
                throw new Exception("No provider available!");

            return provider;
        }

        static void StoreKey(string name, string value)
        {
            IKeyStorageProvider provider = GetKeyStorageProvider();
            provider.StoreKey(name, value);
            
            Console.WriteLine();
            Console.WriteLine("Key stored succesfully");
        }

        static void RetrieveKey(string name)
        {
            IKeyStorageProvider provider = GetKeyStorageProvider();
            string value = provider.RetrieveKeyAsString(name);

            Console.WriteLine();
            if (value != null)
                Console.WriteLine("Value: {0}", value);
            else
                Console.WriteLine("WARNING: Key not found!");

        }

        static void DeleteKey(string name)
        {
            IKeyStorageProvider provider = GetKeyStorageProvider();
            bool keyFound = provider.DeleteKey(name);

            Console.WriteLine();
            if(keyFound)            
                Console.WriteLine("Key deleted succesfully");
            else
                Console.WriteLine("WARNING: Key not found!");
        }

        static void ShowHeader()
        {
            Console.WriteLine("KeyStorageUtility version " + GetVersion());
            Console.WriteLine("Copyright (C) Alessandro Pilotti 2009");
            Console.WriteLine();
            Console.WriteLine("http://www.codeplex.com/KeyStorage");
            Console.WriteLine("info@pilotti.it");
            Console.WriteLine();
            Console.WriteLine("This is free software, you may use it under the terms of");
            Console.WriteLine("the MIT license <http://www.codeplex.com/KeyStorage/license>");
        }

        private static string GetVersion()
        {
            Version version = Assembly.GetExecutingAssembly().GetName().Version;
            return string.Format("{0}.{1}.{2}", version.Major, version.Minor, version.Build);
        }



        static void ShowUsage()
        {
            Console.WriteLine();
            Console.WriteLine("USAGE:");
            Console.WriteLine();
            Console.WriteLine("* To store a key:");
            Console.WriteLine("KeyStorageUtility -s <name> <value>");
            Console.WriteLine();
            Console.WriteLine("* To retrieve a key value:");
            Console.WriteLine("KeyStorageUtility -r <name>");
            Console.WriteLine();
            Console.WriteLine("* To delete a key:");
            Console.WriteLine("KeyStorageUtility -d <name>");
        }

        static void ParseArgs(string[] args)
        {
            if (args.Length == 3 && args[0] == "-s")
                StoreKey(args[1], args[2]);
            else if (args.Length == 2 && args[0] == "-r")
                RetrieveKey(args[1]);
            else if (args.Length == 2 && args[0] == "-d")
                DeleteKey(args[1]);
            else
                ShowUsage();
        }

        static void Main(string[] args)
        {
            try
            {
                ShowHeader();
                ParseArgs(args);
            }
            catch (Exception ex)
            {
                Console.WriteLine();
                Console.WriteLine("ERROR: " + ex.Message);
            }
        }
    }
}
